package com.assess.service.impl;

import com.assess.entity.AssessIndicatorMatrix;
import com.assess.mapper.AssessIndicatorMatrixMapper;
import com.assess.service.AssessIndicatorMatrixService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AssessIndicatorMatrixServiceImpl extends ServiceImpl<AssessIndicatorMatrixMapper, AssessIndicatorMatrix> implements AssessIndicatorMatrixService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatchMatrix(Long parentId, Integer version, List<AssessIndicatorMatrix> elements) {
        if (elements == null || elements.isEmpty()) return;
        for (AssessIndicatorMatrix e : elements) {
            e.setParentId(parentId);
            e.setVersion(version);
        }
        // 覆盖策略：删除同 parentId/version/(schemeId 一致) 后重插
        Long schemeId = elements.get(0).getSchemeId();
        this.remove(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<AssessIndicatorMatrix>()
                .eq(AssessIndicatorMatrix::getParentId, parentId)
                .eq(AssessIndicatorMatrix::getVersion, version)
                .eq(schemeId != null, AssessIndicatorMatrix::getSchemeId, schemeId));
        this.saveBatch(elements);
    }
}


